<?php
// SELECT * FROM menu LEFT JOIN content USING ( id ) WHERE menu.id = ?
//define('GET_CONTENT_BY_ID','SELECT * FROM content, menu WHERE content.id = menu.id AND menu.id = ?');
define('GET_CONTENT_BY_ID','SELECT * FROM menu LEFT JOIN content USING ( id ) WHERE menu.id = ?');

define('INSERT_NEW_ITEM','INSERT INTO `menu`  (`id` ,`value` ,`visibility`,`alias`) VALUES (NULL , ?, ?, ? )');
//define('INSERT_NEW_RELATION','INSERT INTO `menu_relations`  (`id` ,`order_id` ,`parent_id`) VALUES (? , ?, ? )');
define('INSERT_NEW_RELATION','INSERT INTO `menu_relations` ( `id` , `order_id` , `parent_id` ) VALUES ( ? , ((SELECT max(id) FROM menu ) +1), ?)'); 
define('INSERT_NEW_CONTENT','INSERT INTO `content` (`id` ,`text` ,`date`) VALUES (?, ?, now())');

define('DELETE_CONTENT','DELETE FROM `content` WHERE `id` = ?');
define('DELETE_MENU','DELETE FROM `menu` WHERE `id` = ?');
define('DELETE_MENU_RELATIONS','DELETE FROM `menu_relations` WHERE `id` = ?');


define ('MODEL_CONTENT_FIND', 'SELECT * FROM `menu` LEFT JOIN `content` USING ( `id` ) WHERE `menu`.`id` = ?');
		
class Model_Content extends Models_Base {
	
	private $id;
	
	function __construct($id) {
		parent::__construct();	
		
		$this->id = $id;
	}
	
	
	/*function getContentById($id){
		
		$sql = "SELECT * FROM content, menu WHERE content.id = menu.id AND menu.id = $id ";
 		
 		$result = mysql_query($sql,$this->db);
 		
 		if($result) {
 			$content = mysql_fetch_object($result);	
 		}
 		
 		return $content;
		
	}*/
	
	function getContentById($id){
 		$sth = $this->dbh->prepare(GET_CONTENT_BY_ID);
		
 		$sth->execute(array($id));
 		
 		return $sth->fetchObject();
		
	}
	
	function find() {
		return $this->getRow(MODEL_CONTENT_FIND, array($this->id));
	}
	
	/*function saveContent($id,$args) {
		
		$set = '';

		foreach($args as $key => $value) {
			$set .= " `$key` = '$value', ";
		}	
		$set .= 'date = now()';
		$sql = "update content set ".$set." where `id`=$id ";
//		$this->registry['debug']->warn($sql);
		$result = mysql_query($sql,$this->db);
	}*/

	function saveContent($id,$args) {
		
		$set = '';

		foreach($args as $key => $value) {
			$set .= " $key = ?, ";
			$bind[] = $value;
		}	
		$set .= 'date = now()';
		$bind[] = $id;
		$sql = "update content set $set where id = ? ";

		$sth = $this->dbh->prepare($sql);
		return $sth->execute($bind);
	} 



	function addContent($name,$alias,$showorder) {
		$sth = $this->dbh->prepare(INSERT_NEW_ITEM);
		$result = $sth->execute(array(
			$name, 
			$this->registry['setup']->add_condition_visibility ? 1 : 0, 
			$alias
		));
		
		if($result) 
			$id = $this->dbh->lastInsertId();

		error_log("NEW id: $id");
			
		if($id) {	
			$sth2 = $this->dbh->prepare(INSERT_NEW_RELATION);

			$result2 = $sth2->execute(array($id,1));
			
			if($result2) {
				try {
					$sth3 = $this->dbh->prepare(INSERT_NEW_CONTENT);
					$result3 = $sth3->execute(array($id,''));			
				} catch (PDOException $e) {
					print "DB Error!: " . $e->getMessage() . "<br/>";
					die();
				}
				
				$setup = $this->registry['setup'];
				
				if($setup->add_condition_access == 1) {
					$session = $this->registry['session']->sessionInfo();
					$sth4 = $this->dbh->prepare(INSERT_RIGHT);
					$sth4->execute(array($session[user_id], $id));
				}
				
				if($result3) {
					return $alias;
				}
			}
		}
	}
	
	/*
	function addContent($name,$alias,$showorder) {
		$sql = "INSERT INTO `menu`  (`id` ,`value` ,`visibility`,`alias`) VALUES (NULL , '$name', '0' ,'$alias')";	

		$result = mysql_query($sql,$this->db);
		
		$id = mysql_insert_id($this->db);

		if($result) $id = mysql_insert_id($this->db);
			
		if($id) {	

			$sql = "INSERT INTO `menu_relations`  (`id` ,`order_id` ,`parent_id`) VALUES ('$id' , '$showorder', '1')";	

			$result = mysql_query($sql,$this->db);


			$sql = "INSERT INTO `content` (`id` ,`text` ,`date`) VALUES ('$id', '', now())";
			$result = mysql_query($sql,$this->db);

			if($result) {
				return $alias;
			}
		} 
	}
	*/
	
	function deleteContent($id) {
		$sth = $this->dbh->prepare(DELETE_CONTENT);
		$result = $sth->execute(array($id));

		$sth2 = $this->dbh->prepare(DELETE_MENU);
		$result = $sth2->execute(array($id));

		$sth3 = $this->dbh->prepare(DELETE_MENU_RELATIONS);
		$result = $sth3->execute(array($id));
		
		

//		$sql = "DELETE FROM `content` WHERE `id` = $id";
//		$result = mysql_query($sql,$this->db);

/*		$sql = "DELETE FROM `menu` WHERE `id` = $id";
		$result = mysql_query($sql,$this->db);
		
		$sql = "DELETE FROM `menu_relations` WHERE `id` = $id";
		$result = mysql_query($sql,$this->db);*/
				
	}
	
	function addEmptyRow($id) {
		$sth 	= $this->dbh->prepare(INSERT_NEW_CONTENT);
		$result = $sth->execute(array($id,''));		
	}
}

?>
